<?php

class TicketNew extends Controller {

	function TicketNew()
	{
		parent::Controller();	
		log_message('debug', "Ticket: Controler Class Initialized");
		$this->tpl['menu'] = $this->Menu();
		$this->output->enable_profiler(TRUE);
	}

	function Menu()
	{
		$menu[] = anchor('news','News');
		$menu[] = anchor('news/post/','Post Article');
		return $menu;
	}
	
	function index()
	{
		$this->tpl['jsOnLoad']  = '';
		$this->tpl['head'] = 'Help Desk';
		$this->tpl['title'] = 'Help Desk';
      $this->tpl['heading'] = 'Help Desk';
		
		$tno = '59nhnd2fwd'; // This will be selected later on the web site
		
		$ticketViewHead = array (
			'table_open'          => '<table border="1" cellpadding="2" cellspacing="0" width="90%" align="center"><thead>',
			
			'heading_row_start'   => '<tr>',
			'heading_row_end'     => '</tr>',
			'heading_cell_start'  => '<th>',
			'heading_cell_end'    => '</th>',
			
			'row_start'           => '<tr class="odd">',
			'row_end'             => '</tr>',
			'cell_start'          => '<td>',
			'cell_end'            => '</td>',
			
			'row_alt_start'       => '<tr class="even">',
			'row_alt_end'         => '</tr>',
			'cell_alt_start'      => '<td colspan="3">',
			'cell_alt_end'        => '</td>',
			
			'table_close'         => '</thead></table>'
			);
		$ticketViewBody = array (
			'table_open'          => '<table border="1" cellpadding="2" cellspacing="0" width="90%" align="center"><tbody>',
			'heading_row_start'   => '<tr>',
			'heading_row_end'     => '</tr>',
			'heading_cell_start'  => '<th colspan="2">',
			'heading_cell_end'    => '</th>',
			
			'row_start'           => '<tr class="odd">',
			'row_end'             => '</tr>',
			'cell_start'          => '<td  colspan="2">',
			'cell_end'            => '</td>',
			
			'row_alt_start'       => '<tr class="even">',
			'row_alt_end'         => '</tr>',
			'cell_alt_start'      => '<td colspan="6">',
			'cell_alt_end'        => '</td>',
			
			'table_close'         => '</tbody></table>'
			);
		$sql = "SELECT * FROM `Ticket` WHERE `Ticket No` = '$tno'";
		$dbticket = $this->db->query($sql);
		if ($dbticket->num_rows() > 0)
		{
			$tbticketrow = $dbticket->row_array();
			$dbticket->free_result();
		}
		//Get the id so we can load the right messages form the Message table
		$tid = $tbticketrow['id'];
		
		// Remove all id fields
		$tbticketrow = $this->fun->unsetids($tbticketrow);
		// View Ticket table
		$this->load->library('table');
		$this->table->set_template($ticketViewHead);
		$this->table->set_heading(array_keys($tbticketrow));
		$this->table->add_row($tbticketrow);
		$this->tpl['tbticket'] = $this->table->generate();
		$this->table->clear();

		// Get data from Ticket Messages
		$sql = "SELECT * FROM Ticket_Message";
		$dbquery4 = $this->db->query($sql);
		foreach($dbquery4->result_array() as $k => $s):
			$query5[$k] = $s;
		endforeach;
		// Remove id fields
		$query5 = $this->fun->unsetids($query5);
		
		foreach($query5 as $k=>$v){
			$query5[$k]['Date'] = $this->timetamer->ldate($query5[$k]['Date']);
		}

		// Restructure db table to view massages in second row.
		foreach($query5 as $k => $s):
			$query3 = $query5[$k];
			unset($query3['Message']);
			$query6[] = $query3;
			$query6[]['Message'] = $query5[$k]['Message'];
		endforeach;
		
		$this->table->set_template($ticketViewBody);
		$this->table->set_heading(array_keys($query3));
		unset($query3);
		foreach($query6 as $row):
			$this->table->add_row($row);
		endforeach;
		$this->tpl['tbtktmsg'] = $this->table->generate();
		$this->table->clear();
		
		$this->load->view('ticket/view', $this->tpl);
	}
}
?>